#!/usr/bin/perl
# kenesis.pl
use strict;
use warnings;

############################################################################
#    Copyright (C) 2008 by The Kenesis Team <http://code.google.com/p/kenesis/wiki/TheKenesisTeam> #
#    kenesis@googlegroups.com                                                  #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################


#program version
my $VERSION="0.1 Beta 2";

use DB_File;
use Getopt::Long;
my %options;
GetOptions(\%options, "help", "version");

if ($options{version}) {
	version();
} elsif ($options{help}) {
	help();
}

sub version {
	print "\nKenesis $VERSION, build date 05/24/09\n";
	sleep 5;
	exit 0;
}

sub help {
	#help menu will go here
	print "\nHelp is not finished, if you really need help then read the 'readme' file, or the online documentation http://code.google.com/p/kenesis/wiki/howtoinstallandplay\n";
	exit 0;
}

#########################################################################################################################################
#                                                                                                                                       #
#        Main Section - Game start, Player selection, etc...                                                                            #
#                                                                                                                                       #
#########################################################################################################################################

print "Kenesis\n";
sleep 5;

our %player;
our %enemy;

tie %player, 'DB_File', "$ENV{HOME}/.kenesis/player.db" or die "Can't open $ENV{HOME}/.kenesis/player.db: $!\n";

system("clear");
print "Main Menu:\n";
print "New Game\n";
print "Load Game\n";
print "Quit\n";
print ">";
my $option = <STDIN>;
chomp $option;

if ((lc $option) eq "quit") {
	exit;
} elsif ((lc $option) eq "new game") {
	new_game();
} elsif ((lc $option) eq "load game") {
	load_game();
} else {
	print "I don't know $option\n";
	sleep 5;
	exit;
}

# sub load_game {
# 	#new load code goes here, the save file is "$DNV{HOME}/.kenesis
# 	#main_menu();
# 	print "@player\n";
# 	print "@enemy\n";
# 	print "$completed_quests\n";
# 	print "$special_item\n";
# 	print "$class\n";
# 	exit;
# }

sub new_game {
	%player = ( 'hp_max' => '50',
		    'hp' => '50',
		    'mp_max' => '50',
		    'mp' => '50',
		    'strength' => '20',
		    'intelligence' => '20',
		    'wisdom' => '25',
		    'agility' => '30',
		    'defense' => '40',
		    'gold' => '100',
		    'herbs' => '5',
		    'broadsword' => '0',
		    'staffofpower' => '0',
		    'exp' => '0',
		    'strength_potion' => '0',
		    'ether_potion' => '0',
		    'level' => '1',
		    'exp_till_level' => '50',
		    'variable_potion' => '0',
		    'lock_picking' => '30',
		    'completed_Quests' => '0',
		    'special_item' => 'none',
		    'class' => 'null');


	%enemy = ( 'hp' => '30',
		   'attack' => '15',
		   'defense' => '10',
		   'speed' => '15',
		   'agility' => '10',
		   'max_exp' => '10',
		   'max_gold' => '15');

	#Class selection
	system("clear");
	print "Pick a Class:\n";
	print "Fighter\n";
	print "Wizard\n";
	print "Thief\n";
	print "Knight\n";
	print "Paladin\n";
	print ">";
	$player('class') = <STDIN>;
	chomp($player('class'));

	if ((lc $player('class')) eq "fighter") {
		$player('strength') = $player('strength') +8;
		$player('wisdom') = $player('wisdom') - 3;
		$player('class') = "Fighter";
		intro();
	} elsif ((lc $player('class')) eq "wizard") {
		$player('intelligence') = $player('intelligence') + 19;
		$player('mp') = $player('mp') + 20;
		$player('mp_max') = $player('mp');
		$player('defense') = $player('defense') - 3;
		$player('wisdom') = $player('wisdom') + 12;
		$player('ether_potion') = $player('ether_potion') + 2;
		$player('class') = "Wizard";
		intro();
	} elsif ((lc $player('class')) eq "thief") {
		$player('agility') = $player('agility') + 8;
		$player('strength') = $player('strength') - 1;
		$player('lock_picking') = $player('lock_picking') + 8;
		$player('class') = "Thief";
		intro();
	} elsif ((lc $player('class')) eq "knight") {
		$player('defense') = $player('defense') + 5;
		$player('strength') = $player('strength') + 3;
		$player('agility') = $player('agility') - 8;
		$player('class') = "Knight";
		intro();
	} elsif ((lc $player('class')) eq "paladin") {
		$player('hp') = $player('hp') + 5;
		$player('hp_max') = $player('hp');
		$player('mp') = $player('mp') + 2;
		$player('mp_max') = $player('mp');
		$player('wisdom') = $player('wisdom') + 4;
		$player('strength') = $player('strength') + 6;
		$player('agility') = $player('agility') + 2;
		$player('defense') = $player('defense') + 2;
		$player('class') = "Paladin";
		intro();
	} else {
		die "Error: Unknown class, $player('class')";

	}
}

#######################################################################################################################################
#                                                                                                                                     #
#            Game Intro - Sets up the stroy for the player                                                                            #
#                                                                                                                                     #
#######################################################################################################################################

sub intro {
	system("clear");
	print "Hello young adventure, and welcome to the world of Kenesis!\n";
	print "Kenesis use to be a happy and joyous land, but now it is a broken land...\n";
	print "Ever since the \"Creatures\" started attacking, everyone was forced to \nlive our lives in fear\n";
	print "You adventure are these people's last hope to rebuild, and live their \nlife like they use to.\n";
	sleep 30;
	main_menu();
}

#######################################################################################################################################
#                                                                                                                                     #
#           In game menu - Main menu, Shop, Use Item, Stats, Inventory, and game exit                                                 #
#                                                                                                                                     #
#######################################################################################################################################

sub main_menu {
	if ($player[13] >= $player[17]) {
		level();
	} else {
		system("clear");
		print "Menu:\n\n";
		print "Explore \n";
		print "Shop \n";
		print "Use item \n";
		print "Stats. \n";
		print "Inventory \n";
		print "Save Game \n";
		print "Load Game \n";
		print "Quit \n";
		print ">";
		my $option = <STDIN>;
		chomp $option;

		if ((lc $option) eq "quit") {
			system("clear");
			untie(%player);
			exit 0;
		} elsif ((lc $option) eq "explore") {
			explore();
		} elsif ((lc $option) eq "shop") {
			shop();
		} elsif ((lc $option) eq "use item") {
			use_item();
		} elsif ((lc $option) eq "stats") {
			stats();
		} elsif ((lc $option) eq "inventory") {
			inventory();
		} elsif ((lc $option) eq "save game") {
			save_game();
		} elsif ((lc $option) eq "load game") {
			load_game();
		} else {
			print "Error: I don't know that option\n";
			sleep 5;
			main_menu();
		}
	}
}

sub level {
	system("clear");
	#level up subroute goes here
	$player[17] += 50;
	print "You Leveled Up!\n";
	$player[16] += 1;
	$player('hp_max') += 3;
	$player('hp') = $player('hp_max');
	$player('mp_max') += 2;
	$player('mp') = $player('mp_max');
	$player('strength') += 1;
	$player('intelligence') += 2;
	$player('wisdom') += 2;
	$player('agility') += 2;
	$player('defense') += 2;
	sleep 5;
	main_menu();
}

sub use_item {
	system("clear");
	print "Use Item: \n\n";
	print "Herb\n";
	print "Str. Potion\n";
	print "Ether\n";
	print "Var. Potion\n";
	print "-Back-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "herb") {
		herb();
	} elsif ((lc $option) eq "str potion") {
		strength();
	} elsif ((lc $option) eq "ether") {
		ether();
	} elsif ((lc $option) eq "var potion") {
		var_potion();
	} elsif ((lc $option) eq "back") {
		back()
	} else {
		print "Error: I do not understand $option\n";
		sleep 5;
		use_item();
	}
}

sub back {
	main_menu();
}

sub var_potion {
	if ($player[18] >= 1) {
		system("clear");
		print "Which stat: \n\n";
		print "HP\n";
		print "MP\n";
		print "str\n";
		print "wis\n";
		print "def\n";
		print ">";
		my $option = <STDIN>;
		chomp $option;

		if ((lc $option) eq "hp") {
			print "\n\nYou used a variable potion\n";
			print "Max HP went up 5 points\n";
			$player('hp_max') += 5;
			sleep 5;
			main_menu();
		} elsif ((lc $option) eq "mp") {
			print "\n\nYou used a variable potion\n";
			print "Max MP went up 5 points\n";
			$player('mp_max') += 5;
			sleep 5;
			main_menu();
		} elsif ((lc $option) eq "str") {
			print "\n\nYou used a variable potion\n";
			print "Strength went up 5 points\n";
			$player('strength') += 5;
			sleep 5;
			main_menu();

		} elsif ((lc $option) eq "wis") {
			print "\n\nYou used a variable potion\n";
			print "Wisdom went up 5 points\n";
			$player('wisdom') += 5;
			sleep 5;
			main_menu();

		} elsif ((lc $option) eq "def") {
			print "\n\nYou used a variable potion\n";
			print "Defense went up 5 points\n";
			$player('defense') += 5;
			sleep 5;
			main_menu();
		} else {
			print "Error: Unknown stat $option\n";
			sleep 5;
			var_potion();
		}
	} else {
		print "\n\nYou have no Var. Potions to use!\n";
		sleep 5;
		main_menu();
	}
}

sub ether {
	if ($player('ether_potion') >= 1) {
		print "\n\nYou used the ether\n";
		sleep 3;
		print "\nYou regained 10 MP\n";
		$player('mp') += 10;
		$player('ether_potion') -= 1;

		#Check to see MP is not greater than Max MP
		if ($player('mp') >= $player('mp_max') ) {
			$player('mp') = $player('mp_max'); #if is then set MP to equal max mp
			print "\nFull MP restored\n";
			sleep 2;
			print "\nYou now have $player('ether_potion') Ethers left\n";
			sleep 5;
			main_menu();
		} else {
			print "\nYou now have $player('mp') out of $player('mp_max') MP\n";
			sleep 2;
			print "\nYou now have $player('ether_potion') Ethers left\n";
			sleep 5;
			main_menu();
		}
	} else {
		print "\n\nYou have no Ethers to use!\n";
		sleep 5;
		main_menu();
	}
}

sub strength {
	if ( $player[14] >= 1 ) {
		print "\n\nYou used a strength potion\n";
		print "You gained 4 strength points\n";
		$player('strength') += 4;
		$player[14] -= 1;
		sleep 3;
		print "\nYou now have $player[14] Strength potions\n";
		sleep 5;
		main_menu();
	} else {
		print "\n\nYou have no strength potions to use!\n";
		sleep 3;
		main_menu();
	}
}

sub herb {
	if ($player[10] >= 1) {
		print "\n\nYou used a herb\n";
		print "You restored 5 HP\n";
		$player('hp') += 5;
		$player[10] -= 1;
		sleep 3;
		print "\nYou now have $player[10] herbs\n";
		if ($player('hp') > $player('hp_max')) {
			$player('hp') = $player('hp_max');
			print "\nYou now have $player('hp') out of $player('hp_max') HP\n";
			sleep 5;
			main_menu();
		} else {
			print "You now have $player('hp') out of $player('hp_max') HP\n";
			sleep 5;
			main_menu();
		}
	} else {
		print "\n\nYou have no herbs to use!\n";
		sleep 5;
		main_menu();
	}
}

sub stats {
	system("clear");
	print "Your Stats:\n";
	print "Title: $player('class')\n";
	print "Level: $player[16]\n";
	print "Exp: $player[13]\n";
	print "HP: $player('hp')";
	print " / ";
	print "$player('hp_max')\n";
	print "MP: $player('mp')";
	print " / ";
	print "$player('mp_max')\n";
	print "Str: $player('strength')\n";
	print "Def: $player('defense')\n";
	print "Int: $player('intelligence')\n";
	print "Wis: $player('wisdom')\n";
	print "Agl: $player('agility')\n";
	print "Lock Picking: $player('lock_picking')\n";
	print "Gold: $player[9]\n";
	sleep 10;
	main_menu()
}

sub shop {
	system("clear");
	print "Welcome to the shop\n\n";
	print "\nShop Menu\n";
	print "Buy\n";
	print "Sell\n";
	print "Leave\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "buy") {
		print "\n\nBuy what?\n";
		print "Herb: 5 Gold\n";
		print "Str. Potion: 40\n";
		print "Ether: 5\n";
		print "Equipment\n";
		print "Back\n";
		print ">";
		$option = <STDIN>;
		chomp $option;

		if ((lc $option) eq "herb") {
			if ($player[9] >= 5) {
				print "You bought an herb\n";
				$player[9] -= 5;
				$player[10] += 1;
				print "\nYou now have $player[9] gold left\n";
				print "\nYou now have $player[10] herbs\n";

				sleep 5;
				shop();
			} else {
				print "You don't have enough gold\n";
				sleep 5;
				shop();
			}
		} elsif ((lc $option) eq "str. potion") {
			if ($player[9] >= 40) {
				print "You bought an strength potion\n";
				$player[9] -= 40;
				$player[14] += 1;
				print "\n You now have $player[9] gold left\n";
				print "\n You now have $player[14] str. potions\n";
				sleep 5;
				shop();
			} else {
				print "You don't have enough gold\n";
				sleep 5;
				shop();
			}
		} elsif ((lc $option) eq "ether") {
			if ($player[9] >= 5) {
				print "you bought an ether\n";
				$player[9]  -= 5;
				$player('ether_potion') += 1;
				print "\nYou now have $player[9] gold left\n";
				print "\nYou now have $player('ether_potion') Ethers\n";
				sleep 5;
				shop();
			} else {
				print "You don't have enough gold!\n";
				sleep 5;
				shop();
			}
		} elsif ((lc $option) eq "equipment") {
			equipment_buy();
		} elsif ((lc $option) eq "back") {
			shop();
		} else {
			print "\nI don't understand $option!\n";
			sleep 5;
			shop();
		}

	} elsif ((lc $option) eq "sell") {
		print "\n\nSell what?\n";
		print "Equipment\n";
		print "-leave-\n";
		print ">";
		$option = <STDIN>;
		chomp $option;

		if ((lc $option) eq "equipment") {
			equipment_sell();
		} elsif ((lc $option) eq "leave") {
			main_menu()
		} else {
			print "\nI don't understand $option\n";
			sleep 5;
			shop();
		}

	} elsif ((lc $option) eq "leave") {
		main_menu()
	} else {
		print "\nI don't understand $option\n";
		sleep 5;
		shop();
	}
}

sub inventory {
	system("clear");
	print "Inventory\n\n";
	print "\n$player[9] gold\n";
	print "$player[10] herbs\n";
	print "$player[14] Strength potions\n";
	print "$player('ether_potion') ethers\n";
	sleep 5;
	main_menu()
}

sub equipment_sell {
	print "\n\nWeapons\n";
	print "Broadsword\n";
	print "Staff of power\n";
	print "-back-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "broadsword") {
		if ($player[11] >= 1) {
			print "You sold a broadsword\n";
			sleep 2;
			print "Strength dropped by 6 points";
			$player('strength') -= 6;
			$player[9] += 40;
			$player[11] -= 1;
		} else {
			print "You don't have a broadsword to sell\n";
			sleep 10;
			main_menu()
		}
	} elsif ((lc $option) eq "staff of power") {
		if ($player[12] >= 1) {
			print "You sold a staff of power\n";
			sleep 2;
			print "Wisdom dropped by 6 points\n";
			$player('wisdom') -= 6;
			$player[9] += 40;
			$player[12] -= 1;
		} else {
			print "You don't have a staff of power to sell\n";
			sleep 10;
			main_menu()
		}
	} elsif ((lc $option) eq "back") {
		back()
	} else {
		print "I don't understand $option!\n";
		sleep 5;
		equipment_sell();
	}
}

sub equipment_buy {
	print "\n\nWeapions\n";
	print "Broadsword: 80 gold\n";
	print "Staff of Power: 80 gold\n";
	print "-back-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "broadsword") {
		if ($player[11] == 0) {
			if ($player[12] >= 1) {
				print "\nYou may only wield one weapon!\n";
				sleep 10;
				shop();
			} else {
				if ($player[9] >= 80) {
					print "\n You equip the broadsword\n";
					print "Strength went up 6 points\n";
					$player[9] -= 80;
					$player[11] += 1;
					$player('strength') += 4;
					print "You now have $player[9] gold\n";
				} else {
					print "\nYou don't have enough gold!\n";
					sleep 10;
					main_menu();
				}
			}
		} else {
			print "\nYou already have a broadsword!\n";
			sleep 10;
			main_menu();
		}

	} elsif ((lc $option) eq "staff of power") {
		if ($player[12] == 0) {
			if ($player[11] == 0) {
				if ($player[9] >= 80) {
					print "\nYou equipped the staff of power\n";
					print "Wisdom increased by 6 points\n";
					$player[9] -= 80;
					$player[12] += 1;
					$player('wisdom') += 6;
					sleep 10;
					main_menu();
				} else {
					print "\nYou don't have enough gold!\n";
				}
			} else {
				print "\nYou may only wield one weapon!\n";
			}
		} else {
			print "\nYou already have one of these\n";
		}
 	} elsif ((lc $option) eq "back") {
		shop();
	} else {
		print "I don't understand $option!\n";
		sleep 5;
		equipment_buy();
	}
}

sub save_game {
	$cfg->param("player", "@player");
	$cfg->param("quest", "$completed_quests");
	$cfg->param("items", "$special_item");
	$cfg->param("enemy", "@enemy");
	$cfg->param("class", "$player('class')");
	$cfg->write("$ENV{HOME}/.kenesis");
	main_menu();
}

#########################################################################################################################################
#                                                                                                                                       #
#              Explore section                                                                                                          #
#                                                                                                                                       #
#########################################################################################################################################

sub explore {
	system("clear");
	print "Where?\n\n";
	print "Forest\n";
	print "Cave\n";
	print "Swamp\n";
	#add more levels here
	print "-back-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "forest") {
		forest();
	} elsif ((lc $option) eq "cave") {
		cave();
	} elsif ((lc $option) eq "swamp") {
		swamp();
	} elsif ((lc $option) eq "back") {
		back();
	} else {
		print "I don't know $option\n";
		sleep 5;
		explore();
	}
}

sub forest {
	system("clear");
	$player[13] += 2;
	print "Forest Menu:\n\n";
	print "Explore\n";
	print "Quest\n";
	print "-Leave-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "explore") {
		#Explore forest
		print "\n\nYou adventure into the forest...\n";
		my $random_number = int(rand(6));
		if ($random_number == 0) {
			#first enemy
			print "\nA Giant Bat appears\n";
			$enemy[0] = 15;
			$enemy[1] = 18;
			$enemy[2] = 10;
			$enemy[3] = 10;
			$enemy[4] = 10;
			$enemy[5] += 5;
			$enemy[6] += 5;

			sleep 5;
			battle();
		} elsif ($random_number == 1) {
			#second enemy
			print "\nA goblin appears\n";
			$enemy[0] = 10;
			$enemy[1] = 10;
			$enemy[2] = 10;
			$enemy[3] = 10;
			$enemy[4] = 10;

			sleep 5;
			battle();
		} elsif ($random_number == 2) {
			#Third enemy
			print "\nA giant spider appears\n";
			$enemy[0] = 15;
			$enemy[1] = 18;
			$enemy[2] = 15;
			$enemy[3] = 17;
			$enemy[4] = 13;
			$enemy[5] += 5;
			$enemy[6] += 5;

			sleep 5;
			battle();
		} elsif ($random_number == 3) {
			#Fourth enemy
			print "\nA bandit appears\n";
			$enemy[0] = 10;
			$enemy[1] = 10;
			$enemy[2] = 10;
			$enemy[3] = 10;
			$enemy[4] = 10;

			sleep 5;
			battle();
		} elsif ($random_number == 4) {
			#fifth enemy
			print "\nA Hill Giant appears\n";
			$enemy[0] = 30;
			$enemy[1] = 40;
			$enemy[2] = 18;
			$enemy[3] = 26;
			$enemy[4] = 9;
			$enemy[5] += 10;
			$enemy[6] += 10;

			sleep 5;
			battle();
		} elsif ($random_number == 5) {
			#first event
			print "\nBut nothing happens...\n";
			sleep 5;
			main_menu();
		} elsif ($random_number == 6) {
			#second event
			print "\nYou find 10 gold!\n";
			$player[9] += 10;
			sleep 5;
			main_menu();
		} #end of Forest explore option, more will be added later

	} elsif ((lc $option) eq "quest") {
		#Quest forest
		forest_quest();
	} elsif ((lc $option) eq "leave") {
		#go back to explore
		explore();
	} else {
		print "I don't know $option\n";
		sleep 5;
		forest();
	}
}

sub cave {
	system("clear");
	if ($completed_quests == 0) {
		print "You arrive at the entrance of a cave, but the way is blocked.\n";
		sleep 5;
		explore();
	} else {
		print "You arrive at the entrance of a cave, and you notice the entrance is cleared.\n";
	}
	$player[13] += 2;
	print "Cave Menu:\n\n";
	print "Explore\n";
	print "Quest\n";
	print "-Leave-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "explore") {
		#explore the cave
		print "\n\nYou enter into the cave...\n";
		sleep 5;
		my $random_number = int(rand(8));
		if ($random_number == 0) {
			print "\nYou find 10 Gold!\n";
			$player[9] += 10;
			sleep 5;
			main_menu();
		} elsif ($random_number == 1) {
			print "\nBut nothing happens...\n";
			sleep 5;
			main_menu();
		} elsif ($random_number == 2) {
			print "\nYou find a chest!\n";
			sleep 5;
			print "You try to pick the lock\n";
			$random_number = int(rand(3));
			if (($player('lock_picking') + $random_number) >= 15) {
				print "\nYou opened the chest\n";
				sleep 2;
				print "A variable potion was inside the chest\n";
				$player[18] += 1;
				sleep 5;
				main_menu();
			} else {
				print "\nYou failed to pick the lock.\n";
				print "Your lockpicking skill increased by 2 points\n";
				$player('lock_picking') += 2;
				sleep 5;
				main_menu();
			}
		} elsif ($random_number == 3) {
			print "\nA Hill Giant Appears!\n";
			sleep 5;
			$enemy[0] = 30;
			$enemy[1] = 40;
			$enemy[2] = 18;
			$enemy[3] = 26;
			$enemy[4] = 9;
			$enemy[5] += 10;
			$enemy[6] += 10;
			battle();
		} elsif ($random_number == 4) {
			print "\nA Ork Appears!\n";
			sleep 5;
			$enemy[0] = 20;
			$enemy[1] = 32;
			$enemy[2] = 15;
			$enemy[3] = 20;
			$enemy[4] = 10;
			$enemy[5] += 15;
			$enemy[6] += 15;
			battle();
		} elsif ($random_number == 5) {
			print "\nA Cave Spirt Appears!\n";
			sleep 5;
			$enemy[0] = 30;
			$enemy[1] = 40;
			$enemy[2] = 18;
			$enemy[3] = 26;
			$enemy[4] = 9;
			$enemy[5] += 20;
			$enemy[6] += 20;
			battle();
		} elsif ($random_number == 6) {
			print "\nA Skeletos Knight Appears!\n";
			sleep 5;
			$enemy[0] = 30;
			$enemy[1] = 40;
			$enemy[2] = 18;
			$enemy[3] = 26;
			$enemy[4] = 9;
			$enemy[5] += 20;
			$enemy[6] += 20;
			battle();
		} elsif ($random_number == 7) {
			print "\nA Cave Troll Appears!\n";
			sleep 5;
			$enemy[0] = 50;
			$enemy[1] = 50;
			$enemy[2] = 27;
			$enemy[3] = 34;
			$enemy[4] = 12;
			$enemy[5] += 25;
			$enemy[6] += 25;
			battle();
		} else {
			print "\nSome Dark Ooze Appears!\n";
			sleep 5;
			$enemy[0] = 20;
			$enemy[1] = 32;
			$enemy[2] = 15;
			$enemy[3] = 20;
			$enemy[4] = 10;
			$enemy[5] += 15;
			$enemy[6] += 15;
			battle();
		}
	} elsif ((lc $option) eq "quest") {
		#Cave quest
		cave_quest();
	} elsif ((lc $option) eq "leave") {
		#leave
		explore();
	} else {
		print "I don't know $option\n";
		sleep 5;
		cave();
	}
}

sub swamp {
	system("clear");
	if ($completed_quests le 1) {
		print "You arrive at the swamp, but the way is blocked.\n";
		sleep 5;
		explore();
	} else {
		print "You arrive at the swamp, and you notice the way into it is cleared.\n";
	}
	$player[13] += 2;
	print "Swamp Menu:\n\n";
	print "Explore\n";
	print "Quest\n";
	print "-Leave-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "explore") {
		#explore the swamp
		print "\n\nYou enter the swamp...\n";
		sleep 5;
		my $random_number = int(rand(5));
		if ($random_number == 0) {
			print "\n\nYou find 50 Gold!\n";
			sleep 5;
			main_menu();
		} elsif ($random_number == 1) {
			print "\n\nYou wonder around lost in the swamp, but you eventually find your way out.\n";
			sleep 5;
			main_menu();
		} elsif ($random_number == 2) {
			print "\n\nAs you adventure through the swamp you run into the corpse of what to be and ork.\n";
			print "The this isn't anything out of the ordinary, except for the fact that it has a bowie knife in it.\n";
			sleep 2;
			print "\nYou decide to take the bowie knife, you never know when it will come in handy, as you pull the knife out of the corpse the use to be remains fall apart.\n";
			sleep 2;
			print "You take of running before anything could happen to you, but hey you got yourself a new knife!\n";
			$special_item = "bowie knife";
			sleep 5;
			main_menu();
		} elsif ($random_number == 3) {
			print "\n\nAs you adventure through the swamp you see the figure of a man, as you get closer you see that it is David Bowie.  By the time you realize who it is its to late to run because he already spotted you and is heading your way!\n";
			sleep 3;
			system("clear");
			print "Once he catches up with you, he looks you over then states \"I'm David Bowie!\"\n Nothing can kill me... except a bowie knife...\n";
			unless ($special_item eq "bowie knife") {
				sleep 5;
				print "Not having a bowie knife on you, he then begins to sing \"Ziggy Stardust\".  Listening to it is so painful that your ears begin to bleed.\n";
				sleep 5;
				print "After listening to his singing for a couple of minutes you have a massive heart attack!\n";
				sleep 5;
				player_dead();
			} else {
				sleep 5;
				print "Remembering the bowie knife you found earlier you quickly pull it out of your bag and stab it into him!\n";
				print "You have the satisfactory of knowing that you have killed David Bowie, saving the world from at least one evil, but sadly you lost your bowie knife in the process fearing that he is like a vampire and if you remove the knife from his chest he will just come back to life.\n";
				sleep 5;
			}
			main_menu();

		} elsif ($random_number == 4) {
			print "\nYou run into an Ork!\n";
			sleep 5;
			$enemy[0] = 20;
			$enemy[1] = 32;
			$enemy[2] = 15;
			$enemy[3] = 20;
			$enemy[4] = 10;
			$enemy[5] += 15;
			$enemy[6] += 15;
			battle();

		} elsif ($random_number == 5) {
			print "\nA Swamp Troll apears!\n";
			sleep 5;
			$enemy[0] = 40;
			$enemy[1] = 38;
			$enemy[2] = 20;
			$enemy[3] = 21;
			$enemy[4] = 20;
			$enemy[5] += 15;
			$enemy[6] += 15;
			battle();
		}
	} elsif ((lc $option) eq "quest"){
		#Swamp quest
		print "You do the quest...\n";
		sleep 5;
		main_menu();
	} elsif ((lc $option) eq "leave") {
		explore();
	} else {
		print "I don't know $option\n";
		sleep 5;
		swamp();
	}
}

##########################################################################################################################################
#                                                                                                                                        #
#                Battle System                                                                                                           #
#                                                                                                                                        #
##########################################################################################################################################

sub battle {
	system("clear");
	print "Fight Menu\n\n";
	print "Attack\n";
	print "Magic\n";
	#More options goes here
	print "Use Item\n";
	print "Run\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "use item") {
		item_battle();
	} elsif ((lc $option) eq "attack") {
		attack();
	} elsif ((lc $option) eq "run") {
		run();
	} elsif ((lc $option) eq "magic") {
		magic();
	} else {
		print "I don't know that option\n";
		sleep 5;
		battle();
	}
}

sub attack {
	my $player_strength = $player('strength'); #Do not use $player('strength'), player stats must not change!
	my $damage;
	if ($player('agility') >= $enemy[4]) {
		system("clear");
		print "You attack:\n";
		my $attackbonus = int(rand(2));
		my $attackbonus1 = $attackbonus; #$attackbonus1 will serve as a static version attackbonus, there for will not change.
		if (($player_strength + $attackbonus - $enemy[3]) <= 0) {
			print " 0 Damage\n";
			sleep 5;
			enemy_attack();
		} else {
			$enemy[0] -= ($player_strength + $attackbonus - $enemy[3]);
			$damage = $player('strength') + $attackbonus - $enemy[3];
			print " $damage Damage\n";
			sleep 5;
			if ($enemy[0] <= 0) {
				enemy_kill();
			} else {
				enemy_attack();
			}
		}
	} else {
		enemy_preempt();
	}
}

sub enemy_preempt {
	system("clear");
	my $enemy_attack = $enemy[1]; #do not use the enemy array for this
	my $attackbonus = int(rand(2));
	print "Enemy attacks:\n";
	if (($enemy_attack + $attackbonus - $player('defense')) <= 0) {
		print " 0 Damage\n";
		sleep 5;
		player_defense();
	} else {
		$player('hp') -= ($enemy_attack + $attackbonus - $player('defense'));
		my $damage = $enemy_attack + $attackbonus - $player('defense');
		print " $damage Damage\n";
		sleep 5;
		player_defense();
	}
}

sub player_defense {
	system("clear");
	if ($player('hp') <= 0) {
		player_dead();
	}
	print "You attack:\n";
	my $player_strength = $player('strength'); #Do not use $player('strength'), player stats must not change!
	my $damage;
	my $attackbonus = int(rand(2));
	my $attackbonus1 = $attackbonus; #$attackbonus1 will serve as a static version attackbonus, there for will not change.
	if (($player_strength + $attackbonus - $enemy[3]) <= 0) {
		print " 0 Damage\n";
		sleep 5;
		enemy_attack();
	} else {
		$enemy[0] -= ($player_strength + $attackbonus - $enemy[3]);
		$damage = $player('strength') + $attackbonus - $enemy[3];
		print " $damage Damage\n";
		sleep 5;
		if ($enemy[0] <= 0) {
			enemy_kill();
		} else {
			battle_stats();
		}
	}
}

sub enemy_attack {
	system("clear");
	my $enemy_attack = $enemy[1]; #do not use the enemy array for this
	my $attackbonus = int(rand(2));
	print "Enemy attacks:\n";
	if (($enemy_attack + $attackbonus - $player('defense')) <= 0) {
		print " 0 Damage\n";
		sleep 5;
		battle_stats();
	} else {
		$player('hp') -= ($enemy_attack + $attackbonus - $player('defense'));
		my $damage = $enemy_attack + $attackbonus - $player('defense');
		print " $damage Damage\n";
		sleep 5;
		battle_stats();
	}
}

sub battle_stats {
	system("clear");
	print "You: ";
	print "$player('hp')";
	print " / ";
	print "$player('hp_max') HP\n";
	if ($player('hp') <= 0) {
		player_dead();
	} else {
		print "Enemy: $enemy[0]\n";
		sleep 5;
		battle();
	}
}

sub enemy_kill {
	system("clear");
	print "You felled the bastard\n";
	sleep 2;
	my $exp = (int(rand($enemy[5]))) +1;
	my $gold = (int(rand($enemy[6]))) +1;
	print "You get $exp exp.\n";
	print "You get $gold gold\n";
	sleep 2;
	$player[9] += $gold;
	$player[13] += $exp;
	sleep 5;
	main_menu();
}

sub player_dead {
	system("clear");
	print "Game Over!\n";
	print "\n\nYou died!\n";
	exit;
}

sub run {
	system("clear");
	print "You try to run...\n";
	my $try = int(rand(2));
	if ($player('agility') + $try >= $enemy[4]) {
		sleep 5;
		print "You got away!\n";
		sleep 3;
		main_menu();
	} else {
		sleep 5;
		print "But your foe catches up with you\n";
		sleep 3;
		enemy_attack();
	}
}

sub item_battle {
	system("clear");
	print "Which item to use\n";
	print "Herb\n";
	print "Ether\n";
	print "-Leave-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "herb") {
		if ($player[10] >= 1) {
                	print "\n\nYou used a herb\n";
                	print "You restored 5 HP\n";
                	$player('hp') += 5;
                	$player[10] -= 1;

                	sleep 3;
                	print "\nYou now have $player[10] herbs\n";

			if ($player('hp') > $player('hp_max')) {
                        	$player('hp') = $player('hp_max');
                        	print "\nYou now have $player('hp') out of $player('hp_max') HP\n";
                        	sleep 5;
                       		battle();
                	} else {
                        	print "You now have $player('hp') out of $player('hp_max') HP\n";
                        	sleep 5;
                        battle();
                	}
        	} else {
                	print "\n\nYou have no herbs to use!\n";
			sleep 5;
			battle();
		}
	} elsif ((lc $option) eq "ether") {
		if ($player('ether_potion') >= 1) {
                print "\n\nYou used the ether\n";
                sleep 3;
                print "\nYou regained 10 MP\n";
                $player('mp') += 10;
                $player('ether_potion') -= 1;

                #Check to see MP is not greater than Max MP
                	if ($player('mp') >= $player('mp_max') ) {
                        	$player('mp') = $player('mp_max'); #if is then set MP to equal max mp
                        	print "\nFull MP restored\n";
                        	sleep 2;
                        	print "\nYou now have $player('ether_potion') Ethers left\n";
                        	sleep 5;
                        	battle();
                	} else {
                        	print "\nYou now have $player('mp') out of $player('mp_max') MP\n";
                        	sleep 2;
                        	print "\nYou now have $player('ether_potion') Ethers left\n";
                        	sleep 5;
                        	battle();
			}
        	} else {
                	print "\n\nYou have no Ethers to use!\n";
                	sleep 5;
                	battle();
        	}
	} elsif ((lc $option) eq "leave") {
		battle();
	} else {
		print "I don't understand that option $option\n";
		sleep 5;
		item_battle();
	}
}

sub magic {
	unless ((lc $player('class')) eq "wizard" or ((lc $player('class')) eq "paladin")) {
		print "\nYou can not preform any magic!\n";
		sleep 5;
		battle();
	}
	system("clear");
	print "Spell List\n\n";
	print "Fire: 2 MP\n";
	print "Blizzard: 4 MP\n";
	print "Thunder: 8 MP\n";
	#print "Drain: 12 MP\n";
	#print "Fira: 20 MP\n";
	#print "Demi: 25 MP\n";
	#print "Blizzera: 30 MP\n";
	#print "Thundera: 45 MP\n";
	print "-back-\n";
	print ">";
	my $option = <STDIN>;
	chomp $option;

	if ((lc $option) eq "fire") {
		if ($player('mp') >= 2) {
			print "\nYou cast a fire spell\n";
			sleep 5;
			$player('mp') -= 2;

			my $attackbonus_range = $player('wisdom');
			my $attackbonus = $player('agility') + (int(rand($attackbonus_range)));
			if ($attackbonus >= 0) {
				$enemy[0] -= ($player('wisdom') + $attackbonus - $enemy[4] + 2);
				my $damage = $player('wisdom') + $attackbonus - $enemy[4] + 2;
				print " $damage Damage\n";
				print "\nMP = $player('mp')";
				print " / ";
				print "$player('mp_max')\n";

				sleep 5;
				$player('mp') += ($damage/2);

				if ($player('mp') >= $player('mp_max')) {
					$player('mp') = $player('mp_max');
					if ($enemy[0] <= 0) {
						enemy_kill();
					} else {
						enemy_attack();
					}
				} else {
					enemy_attack();
				}
			} else {
				print " 0 Damage\n";
				sleep 5;
				enemy_attack();
			}
		} else {
			print "Not enough MP!\n";
			sleep 5;
			battle();
		}
	} elsif ((lc $option) eq "blizzard") {
		if ($player('mp') >= 4) {
			print "\nYou cast a blizzard spell\n";
			sleep 5;

			$player('mp') -= 4;

			my $attackbonus_range = $player('wisdom');
                        my $attackbonus = $player('agility') + (int(rand($attackbonus_range)));
                        if ($attackbonus >= 0) {
                                $enemy[0] -= ($player('wisdom') + $attackbonus - $enemy
[4] + 4);
                                my $damage = $player('wisdom') + $attackbonus - $enemy[
4] + 4;
                                print " $damage Damage\n";
                                print "\nMP = $player('mp')";
                                print " / ";
                                print "$player('mp_max')\n";

                                sleep 5;
                                $player('mp') += ($damage/2);

                                if ($player('mp') >= $player('mp_max')) {
                                        $player('mp') = $player('mp_max');
                                        if ($enemy[0] <= 0) {
                                                enemy_kill();
                                        } else {
                                                enemy_attack();
                                        }
				 } else {
                                        enemy_attack();
                                 }
                        } else {
                                print " 0 Damage\n";
                                sleep 5;
                                enemy_attack();
                        }
                } else {
                        print "Not enough MP!\n";
                        sleep 5;
                        battle();
                }


	}elsif ((lc $option) eq "thunder") {
		if ($player('mp') >= 8) {
                        print "\nYou cast a thunder spell\n";
                        sleep 5;

                        $player('mp') -= 8;

                        my $attackbonus_range = $player('wisdom');
                        my $attackbonus = $player('agility') + (int(rand($attackbonus_range)));
                        if ($attackbonus >= 0) {
                                $enemy[0] -= ($player('wisdom') + $attackbonus - $enemy[4] + 6);
                                my $damage = $player('wisdom') + $attackbonus - $enemy[
4] + 6;
                                print " $damage Damage\n";
                                print "\nMP = $player('mp')";
                                print " / ";
                                print "$player('mp_max')\n";

                                sleep 5;
                                $player('mp') += ($damage/2);

                                if ($player('mp') >= $player('mp_max')) {
					$player('mp') = $player('mp_max');
                                        if ($enemy[0] <= 0) {
                                                enemy_kill();
                                        } else {
                                                enemy_attack();
                                        }
                                 } else {
                                        enemy_attack();
                                 }
                        } else {
                                print " 0 Damage\n";
                                sleep 5;
                                enemy_attack();
                        }
                } else {
                        print "Not enough MP!\n";
                        sleep 5;
                        battle();
                }


        } elsif ((lc $option) eq "back") {
        	battle();
        } #to add more make this an elsif line!
}

#################################################################################################################################
#                                                                                                                               #
#                    Boss Battle System (Same as above only minus the "run" option)                                             #
#                                                                                                                               #
#################################################################################################################################

sub boss_fight {
        system("clear");
        print "Fight Menu\n\n";
        print "Attack\n";
        print "Magic\n";
        #More options goes here
        print "Use Item\n";
        print ">";
        my $option = <STDIN>;
        chomp $option;

        if ((lc $option) eq "use item") {
                item_boss();
        } elsif ((lc $option) eq "attack") {
               player_attack();
        } elsif ((lc $option) eq "magic") {
                boss_magic();
        } else {
                print "I don't know that option\n";
                sleep 5;
		boss_fight();
        }
}

sub player_attack {
        my $player_strength = $player('strength'); #Do not use $player('strength'), player stats must not change!
        my $damage;
        if ($player('agility') >= $enemy[4]) {
                system("clear");
                print "You attack:\n";
                my $attackbonus = int(rand(2));
                my $attackbonus1 = $attackbonus; #$attackbonus1 will serve as a static version attackbonus, there for will not change.
                if (($player_strength + $attackbonus - $enemy[3]) <= 0) {
                        print " 0 Damage\n";
                        sleep 5;
                        boss_attack();
                } else {
                        $enemy[0] -= ($player_strength + $attackbonus - $enemy[3]);
                        $damage = $player('strength') + $attackbonus - $enemy[3];
                        print " $damage Damage\n";
                        sleep 5;
                        if ($enemy[0] <= 0) {
                                boss_kill();
                        } else {
				boss_attack();
                        }
                }
        } else {
                boss_attack();
        }
}

sub boss_attack {
	 system("clear");
        my $enemy_attack = $enemy[1]; #do not use the enemy array for this
        my $attackbonus = int(rand(2));
        print "Enemy attacks:\n";
        if (($enemy_attack + $attackbonus - $player('defense')) <= 0) {
                print " 0 Damage\n";
                sleep 5;
               bossbattle_stats();
        } else {
                $player('hp') -= ($enemy_attack + $attackbonus - $player('defense'));
                my $damage = $enemy_attack + $attackbonus - $player('defense');
                print " $damage Damage\n";
                sleep 5;
                bossbattle_stats();
        }
}

sub bossbattle_stats {
	 system("clear");
        print "You: ";
        print "$player('hp')";
        print " / ";
        print "$player('hp_max') HP\n";
        if ($player('hp') <= 0) {
                player_dead();
        } else {
                print "Enemy: $enemy[0]\n";
                sleep 5;
                boss_fight();
        }
}

sub boss_kill {
	system("clear");
	print "You kill your foe, and you manage to decapate him, taking his head back to the town for your reward.\n";
	print "You get 100 exp!\n";
	print "You get 200 gold!\n";
	$player[9] += 200;
	$player[13] += 100;
	$completed_quests++;
	sleep 5;
	main_menu();
}

sub item_boss {
	system("clear");
        print "Which item to use\n";
        print "Herb\n";
        print "Ether\n";
        print ">";
        my $option = <STDIN>;
        chomp $option;

        if ((lc $option) eq "herb") {
                if ($player[10] >= 1) {
                        print "\n\nYou used a herb\n";
                        print "You restored 5 HP\n";
                        $player('hp') += 5;
                        $player[10] -= 1;

                        sleep 3;
                        print "\nYou now have $player[10] herbs\n";

                        if ($player('hp') > $player('hp_max')) {
                                $player('hp') = $player('hp_max');
                                print "\nYou now have $player('hp') out of $player[
0] HP\n";
sleep 5;
                                boss_fight();
                        } else {
                                print "You now have $player('hp') out of $player('hp_max') HP\n";
                                sleep 5;
                        boss_fight();
                        }
                } else {
                        print "\n\nYou have no herbs to use!\n";
                        sleep 5;
                        boss_fight();
                }
        } elsif ((lc $option) eq "ether") {
                if ($player('ether_potion') >= 1) {
                print "\n\nYou used the ether\n";
                sleep 3;
                print "\nYou regained 10 MP\n";
                $player('mp') += 10;
                $player('ether_potion') -= 1;

                #Check to see MP is not greater than Max MP
                        if ($player('mp') >= $player('mp_max') ) {
				$player('mp') = $player('mp_max'); #if is then set MP to equal max mp
                                print "\nFull MP restored\n";
                                sleep 2;
                                print "\nYou now have $player('ether_potion') Ethers left\n";
                                sleep 5;
                                boss_fight();
                        } else {
                                print "\nYou now have $player('mp') out of $player('mp_max') MP\n";
                                sleep 2;
                                print "\nYou now have $player('ether_potion') Ethers left\n";
                                sleep 5;
                                boss_fight();
                        }
                } else {
                        print "\n\nYou have no Ethers to use!\n";
                        sleep 5;
                        boss_fight();
                }
        }
}

sub boss_magic {
		unless ((lc $player('class')) eq "wizard" or ((lc $player('class')) eq "paladin")) {
		print "\nYou can not preform any magic!\n";
		sleep 5;
		boss_fight();
	}
	system("clear");
        print "Spell List\n\n";
        print "Fire: 2 MP\n";
        print "Blizzard: 4 MP\n";
        print "Thunder: 8 MP\n";
        #print "Drain: 12 MP\n";
        #print "Fira: 20 MP\n";
        #print "Demi: 25 MP\n";
        #print "Blizzera: 30 MP\n";
        #print "Thundera: 45 MP\n";
        print "-back-\n";
        print ">";
        my $option = <STDIN>;
        chomp $option;

        if ((lc $option) eq "fire") {
                if ($player('mp') >= 2) {
                        print "You cast a fire spell\n";
                        sleep 5;
                        $player('mp') -= 2;

                        my $attackbonus_range = $player('wisdom');
 my $attackbonus = $player('agility') + (int(rand($attackbonus_range)));
                        if ($attackbonus >= 0) {
                                $enemy[0] -= ($player('wisdom') + $attackbonus - $enemy[4] + 2);
                                my $damage = $player('wisdom') + $attackbonus - $enemy[4] + 2;
                                print " $damage Damage\n";
                                print "\nMP = $player('mp')";
                                print " / ";
                                print "$player('mp_max')\n";

                                sleep 5;
                                $player('mp') += ($damage/2);

                                if ($player('mp') >= $player('mp_max')) {
                                        $player('mp') = $player('mp_max');
                                        if ($enemy[0] <= 0) {
                                                enemy_kill();
                                        } else {
                                                enemy_attack();
                                        }
                                } else {
enemy_attack();
                                }
                        } else {
                                print " 0 Damage\n";
                                sleep 5;
                                enemy_attack();
                        }
                } else {
                        print "Not enough MP!\n";
                        sleep 5;
                        battle();
                }
        } elsif ((lc $option) eq "blizzard") {
                if ($player('mp') >= 4) {
                        print "You cast a blizzard spell\n";
                        sleep 5;

                        $player('mp') -= 4;

                        my $attackbonus_range = $player('wisdom');
                        my $attackbonus = $player('agility') + (int(rand($attackbonus_range)));
                        if ($attackbonus >= 0) {
 $enemy[0] -= ($player('wisdom') + $attackbonus - $enemy
[4] + 4);
                                my $damage = $player('wisdom') + $attackbonus - $enemy[
4] + 4;
                                print " $damage Damage\n";
                                print "\nMP = $player('mp')";
                                print " / ";
                                print "$player('mp_max')\n";

                                sleep 5;
                                $player('mp') += ($damage/2);

                                if ($player('mp') >= $player('mp_max')) {
                                        $player('mp') = $player('mp_max');
                                        if ($enemy[0] <= 0) {
                                                enemy_kill();
                                        } else {
                                                enemy_attack();
                                        }
                                 } else {
                                        enemy_attack();
                                 }
                        } else {
                                   print " 0 Damage\n";
                                sleep 5;
                                enemy_attack();
                        }
                } else {
                        print "Not enough MP!\n";
                        sleep 5;
                        battle();
                }


        }elsif ((lc $option) eq "thunder") {
                if ($player('mp') >= 8) {
                        print "You cast a thunder spell\n";
                        sleep 5;

                        $player('mp') -= 8;

                        my $attackbonus_range = $player('wisdom');
                        my $attackbonus = $player('agility') + (int(rand($attackbonus_range)));
                        if ($attackbonus >= 0) {
$enemy[0] -= ($player('wisdom') + $attackbonus - $enemy[4] + 6);
                                my $damage = $player('wisdom') + $attackbonus - $enemy[
4] + 6;
                                print " $damage Damage\n";
                                print "\nMP = $player('mp')";
                                print " / ";
                                print "$player('mp_max')\n";

                                sleep 5;
                                $player('mp') += ($damage/2);

                                if ($player('mp') >= $player('mp_max')) {
                                        $player('mp') = $player('mp_max');
                                        if ($enemy[0] <= 0) {
                                                enemy_kill();
                                        } else {
                                                enemy_attack();
                                        }
                                 } else {
                                        enemy_attack();
                                 }
                        } else {
 print " 0 Damage\n";
                                sleep 5;
                                enemy_attack();
                        }
                } else {
                        print "Not enough MP!\n";
                        sleep 5;
                        battle();
                }


        } #To add more spells, make this an else or elsif
}

######################################################################################################################################
#                                                                                                                                    #
#               Quests Section                                                                                                       #
#                                                                                                                                    #
######################################################################################################################################

sub forest_quest {
	use Term::ReadKey;
	system("clear");
	if ($completed_quests >= 1) {
		print "You already completed this quests...\n";
		sleep 5;
		explore();
	} else {

		#Thanks to the people from perlmonks.org for this great code, for a pager.
		ReadMode 4;         # raw mode
		END{ ReadMode 0 }   # restore mode

		my @data = ("You enter the town near the forest and as you make your way through the town you over hear talk about a goblin base somewhere near the woods.\n", "\nIntrigued you make your way to the village elder to try and get more information about this \"enemy base\".\n", "\nHe tells you where you can find it, and offers you an reward if you can defeat the goblins.\n", "\n\tYou accept the quest and leave the town heading toward the forest.\n", "\nOnce you get to the forest, you head to where the village elder told you to go.\n", "\nWhen you get there you spot a goblin raiding party marching into the entrance of a cave, you figure that they are comming back from a raid.\n", "You decide to try and sneak into the cave, but your sneaking abilities needs a bit of work because after you entered the cave you get spotted by a group of goblins standing guard!\n");
		my $page = 2;
		my $key;
		for my $line (0..$#data) {
    			if ($line and 0 == $line % $page) {
        			1 while not defined ($key=ReadKey(0.5));
        			exit if $key =~ m/q/i;
    			}
    			print $data[$line];
		}
		ReadMode 0; #Restore defaults

		system("clear");
		print "A hill giant approach you, it looks like you are going to have to fight him!\n";
		sleep 5;
		$enemy[0] = 50;
		$enemy[1] = 40;
		$enemy[2] = 18;
		$enemy[3] = 26;
		$enemy[4] = 9;
		boss_fight();
	}
	no Term::ReadKey;
}

sub cave_quest {
	use Term::ReadKey;
	system("clear");
	if ($completed_quests == 2) {
		print "You already completed this quest...\n";
		sleep 5;
		explore();
	} else {
		ReadMode 4;
		END{ ReadMode 0 }

		my @data = ("As you enter the cave, you hear a strange clatter\n", "in the depths of the cave.\n", "As you approach where the noise came from you hear a roar that shakes the whole cavern.\n", "As you look around you notice a flurry of bats pouring out of the chamber to your left.\n", "As you enter the chamber you begin to see a vague light.\n", "You notice that the light is comming from an opening in the cave wall, through it you can see a large fog covered swamp.\n", "Suddenly a giant serpent appears and corners you against the bank.\n", "Its the ceralith that has been terrorizing the nearby village, looks like your going to have to fight it!\n");
		my $page = 1;
		my $key;
		for my $line (0..$#data) {
    			if ($line and 0 == $line % $page) {
        			1 while not defined ($key=ReadKey(0.5));
        			exit if $key =~ m/q/i;
    			}
    			print $data[$line];
		}
		ReadMode 0; #Restore defaults
		system("clear");
		$enemy[0] = 100;
		$enemy[1] = 90;
		$enemy[2] = 70;
		$enemy[3] = 100;
		$enemy[4] = 40;
		boss_fight();
	}
}

__END__

=head1 NAME

Kenesis - Port of a text based RPG originally written for TI-83/TI-84 calculators

=head1 SYNOPSIS

Just execute kenesis the way you normally would for your OS and just type out the action/option that you want to do.

=head1 DESCRIPTION

Kenesis was originally written on the TI-83/TI-84 calculators, by a group of five people in their high school years.  The name is based off of a song composed by Michael Sweeney called "Kinesis".

=head1 SEE ALSO

Perl(1)

=head1 AUTHOR

Adam Jimerson

=cut
